package com.ethink.system.manager;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ethink.framework.common.constant.CommonRespCode;
import com.ethink.framework.common.exception.BusinessException;
import com.ethink.system.api.enums.SystemMenuTypeEnum;
import com.ethink.system.api.info.SystemMenuInfo;
import com.ethink.system.api.info.SystemUserInfo;
import com.ethink.system.api.info.SystemUserSessionInfo;
import com.ethink.system.api.request.SystemUserAddRequest;
import com.ethink.system.common.mapper.SystemMenuMapper;
import com.ethink.system.service.SystemUserService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class SystemUserSessionManager {

    @Autowired
    private SystemUserService systemUserService;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private SystemMenuMapper systemMenuMapper;

    public SystemUserSessionInfo getSystemUserSession(String loginName) {
        SystemUserInfo systemUserInfo = systemUserService.getByLoginName(loginName);
        SystemUserSessionInfo sessionInfo = buildSession(systemUserInfo);
        return sessionInfo;
    }

    public SystemUserSessionInfo loginByAccessToken(String token) {
        Boolean persist = redisTemplate.hasKey("auth:" + token);
        if (persist == null || !persist) {
            throw new BusinessException(CommonRespCode.NOT_LOGIN, "token失效");
        }
        String principal = redisTemplate.opsForValue().get("auth-principal:" + token);
        if (StringUtils.isBlank(principal)) {
            throw new BusinessException(CommonRespCode.NOT_LOGIN, "token用户信息失效");
        }
        JSONObject root = JSON.parseObject(principal);
        SystemUserAddRequest addRequest = new SystemUserAddRequest();
        JSONObject userInfo = root.getJSONObject("globalCustomerInfo");
        addRequest.setLoginName(userInfo.getString("loginName"));
        String realName = userInfo.getString("name");
        addRequest.setRealName(StringUtils.isBlank(realName) ? addRequest.getLoginName() : realName);
        addRequest.setMobileNo(userInfo.getString("mobile"));
        addRequest.setEmail(userInfo.getString("email"));
        addRequest.setUid(userInfo.getString("uid"));
        addRequest.setJobNumber(userInfo.getString("jobNumber"));
        SystemUserInfo systemUserInfo = systemUserService.savaOrUpdate(addRequest);
        SystemUserSessionInfo sessionInfo = buildSession(systemUserInfo);
        return sessionInfo;
    }


    private SystemUserSessionInfo buildSession(SystemUserInfo systemUserInfo) {
        SystemUserSessionInfo sessionInfo = new SystemUserSessionInfo();
        BeanUtils.copyProperties(systemUserInfo, sessionInfo);
        sessionInfo.setSystemUserId(systemUserInfo.getId());
        List<SystemMenuInfo> systemMenus = systemMenuMapper.getMenuBySystemUserId(systemUserInfo.getId());
        List<String> menuPathList = new ArrayList<>();
        List<String> optionList = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(systemMenus)) {
            systemMenus.forEach(systemMenu -> {
                if (SystemMenuTypeEnum.MENU.getIntValue().equals(systemMenu.getType())) {
                    menuPathList.add(systemMenu.getPath());
                } else {
                    optionList.add(systemMenu.getPerms());
                }
            });
        }
        sessionInfo.setMenuPathList(menuPathList);
        sessionInfo.setOptionList(optionList);
        return sessionInfo;
    }
}
